package com.iflytek.web.sevlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ResourceBundle;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.iflytek.domain.Orderitem;
import com.iflytek.service.UserService;
import com.iflytek.utils.PaymentUtil;

@WebServlet("/pay")
public class PayServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	public PayServlet() {
		super();
	}

	public void pay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Orderitem orderitem=new Orderitem();
		ServletContext sc = request.getServletContext();
		WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
		UserService us=(UserService) ac.getBean("userService");
		try {
			BeanUtils.populate(orderitem, request.getParameterMap());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		if(orderitem.getPayway().equals("alipay")){
			//支付宝
			String oiname="测试支付功能";
			orderitem.setOiname(oiname);
			request.setAttribute("orderitem", orderitem);
			request.getRequestDispatcher("alipay_jsp/alipay.trade.page.pay.jsp").forward(request, response);
		}else{
			//银行卡
			String pd_FrpId =orderitem.getPayway();
			String p0_Cmd = "Buy";
			String p1_MerId = ResourceBundle.getBundle("merchantInfo").getString("p1_MerId");
			String p2_Order = orderitem.getOiid();
			//String p3_Amt = orderitem.getCost();
			String p3_Amt = 0.01+"";
			String p4_Cur = "CNY";
			String p5_Pid = "";
			String p6_Pcat = "";
			String p7_Pdesc = "";
			String p8_Url = ResourceBundle.getBundle("merchantInfo").getString("callback");
			String p9_SAF = "";
			String pa_MP = "";
			String pr_NeedResponse = "1";
			String keyValue = ResourceBundle.getBundle("merchantInfo").getString(
					"keyValue");
			String hmac = PaymentUtil.buildHmac(p0_Cmd, p1_MerId, p2_Order, p3_Amt,
					p4_Cur, p5_Pid, p6_Pcat, p7_Pdesc, p8_Url, p9_SAF, pa_MP,
					pd_FrpId, pr_NeedResponse, keyValue);
			String url = "https://www.yeepay.com/app-merchant-proxy/node?pd_FrpId="+pd_FrpId+
					"&p0_Cmd="+p0_Cmd+
					"&p1_MerId="+p1_MerId+
					"&p2_Order="+p2_Order+
					"&p3_Amt="+p3_Amt+
					"&p4_Cur="+p4_Cur+
					"&p5_Pid="+p5_Pid+
					"&p6_Pcat="+p6_Pcat+
					"&p7_Pdesc="+p7_Pdesc+
					"&p8_Url="+p8_Url+
					"&p9_SAF="+p9_SAF+
					"&pa_MP="+pa_MP+
					"&pr_NeedResponse="+pr_NeedResponse+
					"&hmac="+hmac;
			//重定向到第三方支付平台
			response.sendRedirect(url);
		}
		
	}

}
